home *** CD-ROM | disk | FTP | other *** search
/ By Popular Request 2.0 / By Popular Request 2.0 (Arsenal Computer).ISO / amiga_5 / selct102.lha / Select / Source / Select.c < prev    next >
C/C++ Source or Header  |  1995-04-25  |  30KB  |  1,278 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <devices/timer.h>
  5. #include <dos/dos.h>
  6. #include <exec/lists.h>
  7. #include <exec/memory.h>
  8. #include <exec/nodes.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <workbench/workbench.h>
  12. #include <workbench/startup.h>
  13. #include <proto/commodities.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/graphics.h>
  18. #include <proto/icon.h>
  19. #include <proto/intuition.h>
  20.  
  21. /********************************************************************/
  22. /***  Program: Select                                             ***/
  23. /***  Version: 1.02                                               ***/
  24. /***   Author: Torsten Kramer                                     ***/
  25. /***                                                              ***/
  26. /***  Copyright 1995 by Torsten Kramer                            ***/
  27. /***                                                              ***/
  28. /***  Version history:                                            ***/
  29. /***  0.01  - 19.12.1994: - 1. Testversion                        ***/
  30. /***  0.02  - 17.01.1995: - Aktualisierung durch Timer raus       ***/
  31. /***  1.00  - 19.01.1995: - 1. Release                            ***/
  32. /***  1.01  - 21.03.1995: - Aktualisierung durch Timer rein       ***/
  33. /***                      - ESC wird unterstⁿtzt                  ***/
  34. /***                      - Option PUBSCREEN eingebaut            ***/
  35. /***  1.02  - 25.04.1995: - NewLookMenus                          ***/
  36. /***                      - Window hat unter 2.x die richtige     ***/
  37. /***                        Gr÷▀e                                 ***/
  38. /***                      - Window wird auf Default-Publicscreen  ***/
  39. /***                        ge÷ffnet, wenn Option PUBSCREEN nicht ***/
  40. /***                        angegeben ist                         ***/
  41. /********************************************************************/
  42.  
  43. #define PROG_NAME            "Select"
  44. #define PROG_VERSION    "1.02"
  45. #define PROG_DATE            "25.04.95"
  46. #define PROG_DESCR        "Helps to handle screens and windows"
  47.  
  48. #define DEF_HOTKEY        "ctrl alt s"
  49.  
  50. #define TEMPLATE    "CX_POPUP/K,CX_POPKEY/K,CX_PRIORITY/N/K,PUBSCREEN/K"
  51.  
  52. #define OPT_POPUP            0
  53. #define OPT_HOTKEY        1
  54. #define OPT_PRIORITY    2
  55. #define OPT_PUBSCREEN    3
  56. #define NUM_OPTS            4
  57.  
  58. #define WIN_IDCMP        IDCMP_VANILLAKEY | IDCMP_RAWKEY | IDCMP_REFRESHWINDOW | \
  59.                                         IDCMP_CLOSEWINDOW | IDCMP_MENUPICK | LISTVIEWIDCMP
  60.  
  61. #define ID_LISTVIEW    0
  62.  
  63. #define MENU_PROJECT    0
  64. #define ITEM_NEWSHELL    0
  65. #define ITEM_HIDE            1
  66. #define ITEM_ABOUT        2
  67. #define ITEM_QUIT            4
  68.  
  69. #define PRI_WINDOWS                1
  70. #define PRI_TITLE_WINDOWS    2
  71. #define PRI_SCREENS                3
  72. #define PRI_TITLE_SCREENS    4
  73.  
  74. #define EVT_HOTKEY 1L
  75.  
  76. #define    ESC                    0x1b
  77. #define CURSOR_UP        0x4c
  78. #define CURSOR_DOWN    0x4d
  79. #define ENTER                0x0d
  80.  
  81. #define max(a,b) ((a)>(b)?(a):(b))
  82.  
  83. /**********************************************************/
  84. /*  Prototypes                                            */
  85. /**********************************************************/
  86.  
  87. BOOL open_libs(void);
  88. void close_libs(void);
  89. BOOL read_args(int, char **);
  90. BOOL open_device(void);
  91. void close_device(void);
  92. BOOL setup_cx(void);
  93. void free_cx(void);
  94. BOOL open_interface(void);
  95. void close_interface(void);
  96. BOOL gadgets(void);
  97. BOOL menu(void);
  98. void refresh_listview(void);
  99. BOOL create_item_list(void);
  100. void free_nodes(void);
  101. void newshell(void);
  102. void timer(void);
  103. void click_to_front(UWORD);
  104. void select_label(void);
  105. void about(void);
  106. void process_msg(void);
  107.  
  108. /**********************************************************/
  109. /*  Structures                                            */
  110. /**********************************************************/
  111.  
  112. struct NewGadget    ng;
  113.  
  114. struct NewMenu        NewMenu[] =
  115. {
  116.     {NM_TITLE,    "Project",        0        , 0, 0, 0},
  117.     {NM_ITEM,        "Newshell",        "N"    , 0, 0, 0},
  118.     {NM_ITEM,        "Hide",                "H"    , 0, 0, 0},
  119.     {NM_ITEM,        "About...",        "A"    , 0, 0, 0},
  120.     {NM_ITEM,        NM_BARLABEL,    0        , 0, 0, 0},
  121.     {NM_ITEM,        "Quit",                "Q"    , 0, 0, 0},
  122.     {NM_END,        NULL,                    0        , 0, 0, 0}
  123. };
  124.  
  125. struct TextAttr        Topaz80 =
  126. {
  127.     "topaz.font",
  128.     8,
  129.     0,
  130.     0
  131. };
  132.  
  133. struct NewBroker    NewBroker =
  134. {
  135.     NB_VERSION,                                /* nb_Version - Version of the NewBroker structure¢ */
  136.     PROG_NAME,                                /* nb_Name - Name Commodities uses to identify this commodity */
  137.     PROG_NAME,                                /* nb_Title - Title of commodity that appears in CXExchange */
  138.     PROG_DESCR,                                /* nb_Descr - Description of the commodity */
  139.     NBU_UNIQUE | NBU_NOTIFY,    /* nb_Unique - Tells CX not to launch another commodity with same name */
  140.     COF_SHOW_HIDE,                        /* nb_Flags - Tells CX if this commodity has a window */
  141.     0,                                                /* nb_Pri - This commodity's priority */
  142.     0,                                                /* nb_Port - MsgPort CX talks to */
  143.     0                                                    /* nb_ReservedChannel - reserved for later use */
  144. };
  145.  
  146. struct ItemNode
  147. {
  148.     struct Node    node;
  149.     APTR                Item;
  150.     UBYTE                ItemName[80];
  151. };
  152.  
  153.  
  154. /**********************************************************/
  155. /*  Variables                                             */
  156. /**********************************************************/
  157.  
  158. /*---------*/
  159. /* Strings */
  160. /*---------*/
  161.  
  162. char                                    __stdiowin[]        = "CON:0/11/640/80/"
  163.                                                                                  PROG_NAME " " PROG_VERSION;
  164.  
  165. static const char            Version[]                = "\0$VER: " PROG_NAME " "
  166.                                                                                 PROG_VERSION " (" PROG_DATE ")\0";
  167. char                                      Title[80];
  168.  
  169. /*-----------*/
  170. /* Libraries */
  171. /*-----------*/
  172.  
  173. struct Library                 *IconBase                = NULL,
  174.                                             *CxBase                    = NULL,
  175.                                             *GadToolsBase        = NULL;
  176.  
  177. struct GfxBase                *GfxBase                = NULL;
  178. struct IntuitionBase    *IntuitionBase    = NULL;
  179.  
  180. /*---------*/
  181. /* Devices */
  182. /*---------*/
  183.  
  184. struct timerequest        *TimerIO                = NULL;
  185. struct MsgPort                *TimerMP                = NULL;
  186.  
  187. /*-------*/
  188. /* Lists */
  189. /*-------*/
  190.  
  191. struct ItemNode                *WorkNode, *NextNode;
  192. struct List                        *ItemList                = NULL;
  193.  
  194. /*--------------------------------*/
  195. /* Screens, Windows, Gadgets, ... */
  196. /*--------------------------------*/
  197.  
  198. struct Window                    *Win                        = NULL,
  199.                                             *NextWindow,
  200.                                             *ActiveWin;
  201. struct Screen                    *Scr                        = NULL,
  202.                                             *NextScreen;
  203. APTR                                     *VisualInfo         = NULL;
  204. struct Menu                     *MenuStrip            = NULL;
  205. struct Gadget                    *GList                    = NULL,
  206.                                             *ListViewGad;
  207. char                                    GadgetText[]        = "Screen & Window List";
  208. WORD                                    WinLeft                    = 100,
  209.                                             WinTop                    = 50,
  210.                                             WinHeight,
  211.                                             WinWidth;
  212.  
  213. /*-------------*/
  214. /* Commodities */
  215. /*-------------*/
  216.  
  217. CxObj                                    *Broker                    = NULL;
  218. CxMsg                                    *BrokerMsg            = NULL;
  219. struct MsgPort                *BrokerMP                = NULL;
  220.  
  221. /*------*/
  222. /* Misc */
  223. /*------*/
  224.  
  225. int                                     ReturnCode            = RETURN_OK;
  226. LONG                                    DevError                = -1,
  227.                                             OptPriority            = 0;
  228. UWORD                                    NumLabels,
  229.                                             SelectedLabel        = 0;
  230. char                                    OptHotKey[40]        = DEF_HOTKEY,
  231.                                             OptPubScreen[40]= "\0";
  232. BOOL                                    OptPopup                = TRUE,
  233.                                             CxActive,
  234.                                             Interface                = FALSE,
  235.                                             RequestSent            = FALSE;
  236.  
  237.  
  238. /**********************************************************/
  239. /*  main                                                  */
  240. /**********************************************************/
  241.  
  242. int main (int argc, char *argv[])
  243. {
  244.     if (open_libs())
  245.     {
  246.         if (read_args(argc, argv))
  247.         {
  248.             if (open_device())
  249.             {
  250.                 if (setup_cx())
  251.                 {
  252.                     if (OptPopup)
  253.                         open_interface();
  254.  
  255.                     if ((OptPopup && Interface) || (! OptPopup))
  256.                         process_msg();
  257.                 }
  258.             }
  259.         }
  260.     }
  261.     close_interface();
  262.     free_cx();
  263.     close_device();
  264.     close_libs();
  265.  
  266.     return(ReturnCode);
  267. }
  268.  
  269. /**********************************************************/
  270. /*  open_libs                                             */
  271. /**********************************************************/
  272.  
  273. BOOL open_libs(void)
  274. {
  275.     if ((CxBase = OpenLibrary("commodities.library", 37)) != NULL)
  276.     {
  277.         if ((GadToolsBase = OpenLibrary("gadtools.library", 37)) != NULL)
  278.         {
  279.             if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37)) != NULL)
  280.             {
  281.                 if ((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37)) != NULL)
  282.                 {
  283.                     if ((IconBase = OpenLibrary("icon.library", 37)) != NULL)
  284.                     {
  285.                         return(TRUE);
  286.                     }
  287.                 }
  288.             }
  289.         }
  290.     }
  291.  
  292.     PrintFault(ERROR_INVALID_RESIDENT_LIBRARY, NULL);
  293.     ReturnCode = RETURN_FAIL;
  294.  
  295.     return(FALSE);
  296. }
  297.  
  298. /**********************************************************/
  299. /*  close_libs                                            */
  300. /**********************************************************/
  301.  
  302. void close_libs(void)
  303. {
  304.     if (CxBase                != NULL)    CloseLibrary(CxBase);
  305.     if (GadToolsBase    != NULL)    CloseLibrary(GadToolsBase);
  306.     if (IntuitionBase    != NULL)    CloseLibrary((struct Library *)IntuitionBase);
  307.     if (GfxBase                != NULL)    CloseLibrary((struct Library *)GfxBase);
  308.     if (IconBase            != NULL)    CloseLibrary(IconBase);
  309. }
  310.  
  311. /**********************************************************/
  312. /*  read_args                                             */
  313. /**********************************************************/
  314.  
  315. BOOL read_args(int argc, char *argv[])
  316. {
  317.     struct WBStartup    *WBenchMsg;
  318.     struct WBArg            *WBenchArg;
  319.  
  320.     struct DiskObject *DiskObj;
  321.     char                            **ToolArray, *ToolValue;
  322.  
  323.     struct RDArgs            *RDA;
  324.     LONG                            OptArray[NUM_OPTS], Error, OldDir;
  325.  
  326.     char                            No[]    = "NO";
  327.  
  328.     /* Set default values */
  329.  
  330.     OptArray[OPT_POPUP]            = (LONG)"YES";
  331.     OptArray[OPT_HOTKEY]        = NULL;
  332.     OptArray[OPT_PRIORITY]    = 0;
  333.     OptArray[OPT_PUBSCREEN]    =    NULL;
  334.  
  335.     if (argc == 0)
  336.     {
  337.         /* The program was started from the Workbench */
  338.  
  339.         WBenchMsg         = (struct WBStartup *)argv;
  340.         WBenchArg            = WBenchMsg->sm_ArgList;
  341.  
  342.         /* Process only the first argument (our tool). Ignore */
  343.         /* any additional args which are icons passed to us   */
  344.         /* via either extend select or default tool method.   */
  345.  
  346.         if (WBenchArg->wa_Name != NULL)
  347.         {
  348.             /* Change to the directory where the file is stored */
  349.  
  350.             if (WBenchArg->wa_Lock != NULL)
  351.             {
  352.                 OldDir = CurrentDir(WBenchArg->wa_Lock);
  353.             }
  354.             else
  355.                 OldDir = -1;
  356.  
  357.             /* Read DiskObject structure (.info file) */
  358.  
  359.             if ((DiskObj = GetDiskObject(WBenchArg->wa_Name)) != NULL)
  360.             {
  361.                 /* Get the pointer to the ToolType array */
  362.  
  363.                 ToolArray = DiskObj->do_ToolTypes;
  364.  
  365.                 /* Check for ToolTypes we need and get the */
  366.                 /* value(s) of the ToolTypes.              */
  367.  
  368.                 if ((ToolValue = FindToolType(ToolArray, "CX_POPUP")) != NULL)
  369.                     if (MatchToolValue(ToolValue, No))
  370.                         OptPopup = FALSE;
  371.  
  372.                 if ((ToolValue = FindToolType(ToolArray, "CX_POPKEY")) != NULL)
  373.                     if (strlen(ToolValue) < 40)
  374.                         strcpy(OptHotKey, ToolValue);
  375.  
  376.                 if ((ToolValue = FindToolType(ToolArray, "CX_PRIORITY")) != NULL)
  377.                     OptPriority = atol(ToolValue);
  378.  
  379.                 if ((ToolValue = FindToolType(ToolArray, "PUBSCREEN")) != NULL)
  380.                     if (strlen(ToolValue) < 40)
  381.                         strcpy(OptPubScreen, ToolValue);
  382.  
  383.                 FreeDiskObject(DiskObj);
  384.             }
  385.  
  386.             /* Change back to the original directory */
  387.  
  388.             if (OldDir != -1)
  389.                 CurrentDir(OldDir);
  390.         }
  391.     }
  392.     else
  393.     {
  394.         /* The program was started from the Shell */
  395.  
  396.         if ((RDA = ReadArgs(TEMPLATE, OptArray, NULL)))
  397.         {
  398.             /* The user typed a valid command line */
  399.  
  400.             if (stricmp((char *)OptArray[OPT_POPUP], No) == NULL)
  401.                 OptPopup = FALSE;
  402.  
  403.             if (OptArray[OPT_HOTKEY] != NULL)
  404.                 if (strlen((char *)OptArray[OPT_HOTKEY]) < 40)
  405.                     strcpy(OptHotKey, (char *)OptArray[OPT_HOTKEY]);
  406.             
  407.             if (OptArray[OPT_PUBSCREEN] != NULL)
  408.                 if (strlen((char *)OptArray[OPT_PUBSCREEN]) < 40)
  409.                     strcpy(OptPubScreen, (char *)OptArray[OPT_PUBSCREEN]);
  410.  
  411.             OptPriority = OptArray[OPT_PRIORITY];
  412.  
  413.             FreeArgs(RDA);
  414.         }
  415.         else
  416.         {
  417.             /* There is an error in the command line */
  418.  
  419.             Error = IoErr();
  420.             PrintFault(Error, argv[0]);
  421.             ReturnCode = RETURN_FAIL;
  422.  
  423.             return(FALSE);
  424.         }
  425.     }
  426.  
  427.     return(TRUE);
  428. }
  429.  
  430. /**********************************************************/
  431. /*  open_device                                           */
  432. /**********************************************************/
  433.  
  434. BOOL open_device(void)
  435. {
  436.     if (ItemList = AllocMem(sizeof(struct List), MEMF_PUBLIC | MEMF_CLEAR))
  437.     {
  438.         NewList(ItemList);
  439.         if (TimerMP = CreateMsgPort())
  440.         {
  441.             if (TimerIO = (struct timerequest *)CreateIORequest(TimerMP, sizeof(struct timerequest)))
  442.             {
  443.                 if ((DevError = OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)TimerIO, 0)) == 0)
  444.                 {
  445.                     return(TRUE);
  446.                 }
  447.             }
  448.         }
  449.     }
  450.  
  451.     ReturnCode = RETURN_FAIL;
  452.  
  453.     return(FALSE);
  454. }
  455.  
  456. /**********************************************************/
  457. /*  close_device                                          */
  458. /**********************************************************/
  459.  
  460. void close_device(void)
  461. {
  462.     if (RequestSent)
  463.     {
  464.         if (CheckIO((struct IORequest *)TimerIO) == NULL)
  465.         {
  466.           AbortIO((struct IORequest *)TimerIO);
  467.         }
  468.         WaitIO((struct IORequest *)TimerIO);
  469.     }
  470.  
  471.     if (DevError    != -1)        CloseDevice((struct IORequest *)TimerIO);
  472.     if (TimerIO         != NULL)    DeleteIORequest(TimerIO);
  473.     if (TimerMP         != NULL)    DeleteMsgPort(TimerMP);
  474.  
  475.     if (ItemList != NULL)
  476.     {
  477.         free_nodes();
  478.         if (ItemList     != NULL)    FreeMem(ItemList, sizeof(struct List));
  479.     }
  480. }
  481.  
  482. /**********************************************************/
  483. /*  setup_cx                                              */
  484. /**********************************************************/
  485.  
  486. BOOL setup_cx(void)
  487. {
  488.     CxObj *Filter, *Sender, *Translate;
  489.  
  490.     if ((BrokerMP = CreateMsgPort()))
  491.     {
  492.         if (OptPriority < -128 || OptPriority > 127)
  493.             OptPriority = 0;
  494.  
  495.         NewBroker.nb_Pri    = OptPriority;
  496.         NewBroker.nb_Port    = BrokerMP;
  497.  
  498.         /* create the title of the cx window */
  499.         
  500.         sprintf(Title, "%s: Hot Key = <%s>", PROG_NAME, OptHotKey);
  501.  
  502.         if ((Broker = CxBroker(&NewBroker, NULL)))
  503.         {
  504.             if ((Filter = CxFilter(OptHotKey)))
  505.             {
  506.                 AttachCxObj(Broker, Filter);
  507.                 if ((Sender = CxSender(BrokerMP, EVT_HOTKEY)))
  508.                 {
  509.                     AttachCxObj(Filter, Sender);
  510.                     if ((Translate = CxTranslate(NULL)))
  511.                     {
  512.                         AttachCxObj(Filter, Translate);
  513.                         if (CxObjError(Filter) == NULL)
  514.                         {
  515.                             ActivateCxObj(Broker, 1);
  516.                             CxActive = TRUE;
  517.                             return(TRUE);
  518.                         }
  519.                     }
  520.                 }
  521.             }
  522.         }
  523.     }
  524.  
  525.     ReturnCode = RETURN_FAIL;
  526.  
  527.     return(FALSE);
  528. }
  529.  
  530. /**********************************************************/
  531. /*  free_cx                                               */
  532. /**********************************************************/
  533.  
  534. void free_cx(void)
  535. {
  536.     if (Broker != NULL)
  537.         DeleteCxObjAll(Broker);
  538.  
  539.     if (BrokerMP != NULL)
  540.     {
  541.         while ((BrokerMsg = (CxMsg *)GetMsg(BrokerMP)))
  542.             ReplyMsg((struct Message *)BrokerMsg);
  543.  
  544.         DeletePort(BrokerMP);
  545.     }
  546. }
  547.  
  548. /**********************************************************/
  549. /*  open_interface                                        */
  550. /**********************************************************/
  551.  
  552. BOOL open_interface(void)
  553. {
  554.     UWORD                            DispWidth, DispHeight;
  555.     ULONG                            ScreenModeID;
  556.     struct Rectangle    Rect;
  557.  
  558.     if (IntuitionBase->ActiveWindow != Win)
  559.         ActiveWin = IntuitionBase->ActiveWindow;
  560.     
  561.     if (Win == NULL)
  562.     {
  563.         if ((Scr = LockPubScreen(OptPubScreen)) == NULL)
  564.             Scr = LockPubScreen(NULL);
  565.             
  566.         if (Scr)
  567.         {
  568.             if (VisualInfo = GetVisualInfo(Scr, TAG_END))
  569.             {
  570.                 if (gadgets())
  571.                 {
  572.                     if (menu())
  573.                     {
  574.                         if((ScreenModeID = GetVPModeID(&Scr->ViewPort)) != INVALID_ID)
  575.                         {
  576.                             if(QueryOverscan(ScreenModeID, &Rect, OSCAN_TEXT))
  577.                             {
  578.                                 DispWidth = Rect.MaxX - Rect.MinX + 1;
  579.                                 DispHeight = Rect.MaxY - Rect.MinY + 1;
  580.  
  581.                                 WinLeft = Scr->MouseX;
  582.                                 WinTop = Scr->MouseY;
  583.  
  584.                                 if (WinLeft < -Scr->LeftEdge)
  585.                                 {
  586.                                     WinLeft = max(0, -Scr->LeftEdge);
  587.                                     WinLeft = WinLeft + 100;
  588.                                 }
  589.  
  590.                                 if (WinTop < -Scr->TopEdge)
  591.                                 {
  592.                                     WinTop = max(0, -Scr->TopEdge);
  593.                                     WinTop = WinTop + 50;
  594.                                 }
  595.  
  596.                                 if ((WinLeft + WinWidth) > (DispWidth + -Scr->LeftEdge))
  597.                                 {
  598.                                     WinLeft = (DispWidth + -Scr->LeftEdge) - WinWidth;
  599.                                 }
  600.  
  601.                                 if ((WinTop + WinHeight) > (DispHeight + -Scr->TopEdge))
  602.                                 {
  603.                                     WinTop = (DispHeight + -Scr->TopEdge) - WinHeight;
  604.                                 }
  605.                             }
  606.                         }
  607.  
  608.                         if (Win = OpenWindowTags(NULL,
  609.                                 WA_PubScreen, Scr,
  610.                                 WA_Left, WinLeft,
  611.                                 WA_Top, WinTop,
  612.                                 WA_Height, WinHeight,
  613.                                 WA_Width, WinWidth,
  614.                                 WA_Gadgets, GList,
  615.                                 WA_Title, Title,
  616.                                 WA_IDCMP, WIN_IDCMP,
  617.                                 WA_NewLookMenus, TRUE,
  618.                                 WA_Flags, WFLG_CLOSEGADGET | WFLG_DEPTHGADGET |
  619.                                                     WFLG_ACTIVATE | WFLG_DRAGBAR | WFLG_SMART_REFRESH,
  620.                                 TAG_DONE))
  621.                         {
  622.                             GT_RefreshWindow(Win, NULL);
  623.                             if(SetMenuStrip(Win, MenuStrip))
  624.                             {
  625.                                 WindowToFront(Win);
  626.                                 ScreenToFront(Win->WScreen);
  627.                                 Interface = TRUE;
  628.                                 refresh_listview();
  629.                                 return(TRUE);
  630.                             }
  631.                         }
  632.                     }
  633.                 }
  634.             }
  635.         }
  636.         close_interface();
  637.         return(FALSE);
  638.     }
  639.     else
  640.     {
  641.         WindowToFront(Win);
  642.         ActivateWindow(Win);
  643.         ScreenToFront(Win->WScreen);
  644.         refresh_listview();
  645.         return(TRUE);
  646.     }
  647. }
  648.  
  649.  
  650. /**********************************************************/
  651. /*  close_interface                                       */
  652. /**********************************************************/
  653.  
  654. void close_interface(void)
  655. {
  656.     if(Win != NULL)
  657.     {
  658.         ClearMenuStrip(Win);
  659.         CloseWindow(Win);
  660.         Win = NULL;
  661.     }
  662.  
  663.     if (MenuStrip != NULL)
  664.     {
  665.         FreeMenus(MenuStrip);
  666.         MenuStrip = NULL;
  667.     }
  668.  
  669.     if (GList != NULL)
  670.     {
  671.         FreeGadgets(GList);
  672.         GList = NULL;
  673.     }
  674.  
  675.     if (VisualInfo != NULL)
  676.     {
  677.         FreeVisualInfo(VisualInfo);
  678.         VisualInfo = NULL;
  679.     }
  680.  
  681.     if (Scr != NULL)
  682.     {
  683.         UnlockPubScreen(NULL, Scr);
  684.         Scr = NULL;
  685.     }
  686.  
  687.     Interface = FALSE;
  688. }
  689.  
  690. /**********************************************************/
  691. /*  gadgets                                               */
  692. /**********************************************************/
  693.  
  694. BOOL gadgets(void)
  695. {
  696.     WORD                    PixLength;
  697.     struct Gadget *Gad;
  698.     
  699.     PixLength = TextLength(&Scr->RastPort, GadgetText, strlen(GadgetText));
  700.  
  701.     /* Listview gadget */
  702.  
  703.     ng.ng_LeftEdge        = 16;
  704.     ng.ng_TopEdge            = Scr->WBorTop + (Scr->Font->ta_YSize * 2 + INTERHEIGHT * 2);
  705.     ng.ng_Width                = PixLength * 2;
  706.     ng.ng_Height            = Scr->Font->ta_YSize * 16;
  707.     ng.ng_GadgetText    = GadgetText;
  708.     ng.ng_TextAttr        = Scr->Font;
  709.     ng.ng_GadgetID        = ID_LISTVIEW;
  710.     ng.ng_Flags                = 0;
  711.     ng.ng_UserData        = NULL;
  712.     ng.ng_VisualInfo    = VisualInfo;
  713.  
  714.     Gad = CreateContext(&GList);
  715.     Gad = ListViewGad = CreateGadget(LISTVIEW_KIND, Gad, &ng,
  716.                                                                      GTLV_Labels, NULL,
  717.                                                                      GTLV_ShowSelected, NULL,
  718.                                                                      GTLV_Selected, 0,
  719.                                                                      TAG_END);
  720.  
  721.     if (Gad != NULL)
  722.     {
  723.         WinHeight    = ng.ng_TopEdge + ng.ng_Height + INTERHEIGHT * 2;
  724.         WinWidth    = ng.ng_LeftEdge + ng.ng_Width + INTERWIDTH * 2;
  725.         return(TRUE);
  726.     }
  727.     else
  728.         return(FALSE);
  729. }
  730.  
  731. /**********************************************************/
  732. /*  menu                                                  */
  733. /**********************************************************/
  734.  
  735. BOOL menu(void)
  736. {
  737.     if ((MenuStrip = CreateMenus(NewMenu, TAG_END)))
  738.     {
  739.         if (LayoutMenus(MenuStrip, VisualInfo,
  740.                     GTMN_NewLookMenus, TRUE,
  741.                     TAG_END))
  742.         {
  743.             return(TRUE);
  744.         }
  745.     }
  746.  
  747.     return(FALSE);
  748. }
  749.  
  750. /**********************************************************/
  751. /*  refresh_listview                                      */
  752. /**********************************************************/
  753.  
  754. void refresh_listview(void)
  755. {
  756.     GT_SetGadgetAttrs(ListViewGad, Win, NULL,
  757.                                         GTLV_Labels, ~0,
  758.                                         TAG_END);
  759.  
  760.     if (create_item_list())
  761.     {
  762.         if (SelectedLabel > (NumLabels - 1))
  763.             SelectedLabel = NumLabels - 1;
  764.             
  765.             GT_SetGadgetAttrs(ListViewGad, Win, NULL,
  766.                                                 GTLV_Labels, ItemList,
  767.                                                 GTLV_Selected, SelectedLabel,
  768.                                                 TAG_END);
  769.     }
  770. }
  771.  
  772. /**********************************************************/
  773. /*  create_item_list                                      */
  774. /**********************************************************/
  775.  
  776. BOOL create_item_list(void)
  777. {
  778.     free_nodes();
  779.     NumLabels = 0;
  780.  
  781.     if ((WorkNode = AllocMem(sizeof(struct ItemNode), MEMF_PUBLIC | MEMF_CLEAR)))
  782.     {
  783.         WorkNode->node.ln_Pri        = PRI_TITLE_SCREENS;
  784.         WorkNode->node.ln_Type    = NT_USER;
  785.         WorkNode->Item                    = NULL;
  786.  
  787.         strcpy(WorkNode->ItemName, "(Screens)");
  788.         WorkNode->node.ln_Name    = WorkNode->ItemName;
  789.  
  790.         Enqueue(ItemList, (struct Node *)WorkNode);
  791.         NumLabels++;
  792.  
  793.         if ((WorkNode = AllocMem(sizeof(struct ItemNode), MEMF_PUBLIC | MEMF_CLEAR)))
  794.         {
  795.             WorkNode->node.ln_Pri        = PRI_TITLE_WINDOWS;
  796.             WorkNode->node.ln_Type    = NT_USER;
  797.             WorkNode->Item                    = NULL;
  798.  
  799.             strcpy(WorkNode->ItemName, "(Windows)");
  800.             WorkNode->node.ln_Name    = WorkNode->ItemName;
  801.  
  802.             Enqueue(ItemList, (struct Node *)WorkNode);
  803.             NumLabels++;
  804.  
  805.             if (IntuitionBase->FirstScreen != NULL)
  806.             {
  807.                 NextScreen = IntuitionBase->FirstScreen;
  808.  
  809.                 while (NextScreen != NULL)
  810.                 {
  811.                     if ((WorkNode = AllocMem(sizeof(struct ItemNode), MEMF_PUBLIC | MEMF_CLEAR)))
  812.                     {
  813.                         WorkNode->node.ln_Pri        = PRI_SCREENS;
  814.                         WorkNode->node.ln_Type    = NT_USER;
  815.                         WorkNode->Item                    = NextScreen;
  816.  
  817.                         strcpy(WorkNode->ItemName, "  ");
  818.  
  819.                         if (NextScreen->Title != NULL)
  820.                             strncat(WorkNode->ItemName, NextScreen->Title, 77);
  821.                         else
  822.                             strcat(WorkNode->ItemName, "<no title>");
  823.  
  824.                         WorkNode->node.ln_Name    = WorkNode->ItemName;
  825.  
  826.                         Enqueue(ItemList, (struct Node *)WorkNode);
  827.                         NumLabels++;
  828.  
  829.                         if (NextScreen->FirstWindow != NULL)
  830.                         {
  831.                             NextWindow = NextScreen->FirstWindow;
  832.  
  833.                             while (NextWindow != NULL)
  834.                             {
  835.                                 if ((WorkNode = AllocMem(sizeof(struct ItemNode), MEMF_PUBLIC | MEMF_CLEAR)))
  836.                                 {
  837.                                     WorkNode->node.ln_Pri     = PRI_WINDOWS;
  838.                                     WorkNode->node.ln_Type = NT_USER;
  839.                                     WorkNode->Item                 = NextWindow;
  840.  
  841.                                     if (NextWindow != ActiveWin)
  842.                                         strcpy(WorkNode->ItemName, "  ");
  843.                                     else
  844.                                         strcpy(WorkNode->ItemName, "> ");
  845.  
  846.                                     if (NextWindow->Title != NULL)
  847.                                         strncat(WorkNode->ItemName, NextWindow->Title, 77);
  848.                                     else
  849.                                         strcat(WorkNode->ItemName, "<no title>");
  850.  
  851.                                     WorkNode->node.ln_Name = WorkNode->ItemName;
  852.  
  853.                                     Enqueue(ItemList, (struct Node *)WorkNode);
  854.                                     NumLabels++;
  855.  
  856.                                     NextWindow = NextWindow->NextWindow;
  857.                                 }
  858.                                 else
  859.                                     return(FALSE);
  860.                             }
  861.                         }
  862.                         NextScreen = NextScreen->NextScreen;
  863.                     }
  864.                     else
  865.                         return(FALSE);
  866.                 }
  867.             }
  868.         }
  869.         else
  870.             return(FALSE);
  871.     }
  872.     else
  873.         return(FALSE);
  874.  
  875.     return(TRUE);
  876. }
  877.  
  878.  
  879. /**********************************************************/
  880. /*  free_nodes                                            */
  881. /**********************************************************/
  882.  
  883. void free_nodes(void)
  884. {
  885.     WorkNode = (struct ItemNode *)ItemList->lh_Head;
  886.  
  887.     while ((NextNode = (struct ItemNode *)WorkNode->node.ln_Succ))
  888.     {
  889.         Remove((struct Node *)WorkNode);
  890.         FreeMem(WorkNode, sizeof(struct ItemNode));
  891.         WorkNode = NextNode;
  892.     }
  893. }
  894.  
  895. /**********************************************************/
  896. /*  newshell                                              */
  897. /**********************************************************/
  898.  
  899. void newshell(void)
  900. {
  901.     Execute("Newshell", NULL, NULL);
  902. }
  903.  
  904. /**********************************************************/
  905. /*  timer                                                 */
  906. /**********************************************************/
  907.  
  908. void timer(void)
  909. {
  910.     if (CxActive && Interface)
  911.     {
  912.         TimerIO->tr_node.io_Command    = TR_ADDREQUEST;
  913.         TimerIO->tr_time.tv_secs        = 1;
  914.         TimerIO->tr_time.tv_micro        = 0;
  915.  
  916.         SendIO((struct IORequest *)TimerIO);
  917.  
  918.         RequestSent = TRUE;
  919.     }
  920. }
  921.  
  922. /**********************************************************/
  923. /*  click_to_front                                        */
  924. /**********************************************************/
  925.  
  926. void click_to_front(UWORD ItemNumber)
  927. {
  928.     struct Window *FirstWindow;
  929.     
  930.     WorkNode = (struct ItemNode *)ItemList->lh_Head;
  931.  
  932.     for (ItemNumber; ItemNumber > 0; ItemNumber--)
  933.         WorkNode = (struct ItemNode *)WorkNode->node.ln_Succ;
  934.  
  935.     switch (WorkNode->node.ln_Pri)
  936.     {
  937.         case PRI_WINDOWS:
  938.             if ((struct Window *)WorkNode->Item != Win)
  939.             {
  940.                 ScreenToFront(((struct Window *)WorkNode->Item)->WScreen);
  941.                 WindowToFront((struct Window *)WorkNode->Item);
  942.                 ActivateWindow((struct Window *)WorkNode->Item);
  943.                 Interface = FALSE;
  944.             }
  945.             break;
  946.  
  947.         case PRI_SCREENS:
  948.             ScreenToFront((struct Screen *)WorkNode->Item);
  949.             
  950.             if (((struct Screen *)WorkNode->Item)->FirstWindow != Win)
  951.                 FirstWindow = ((struct Screen *)WorkNode->Item)->FirstWindow;
  952.             else
  953.                 FirstWindow = ((struct Screen *)WorkNode->Item)->FirstWindow->NextWindow;
  954.                 
  955.             if (FirstWindow != NULL)
  956.             {
  957.                 WindowToFront(FirstWindow);
  958.                 ActivateWindow(FirstWindow);
  959.             }
  960.             Interface = FALSE;
  961.             break;
  962.  
  963.         default:
  964.             break;
  965.     }
  966. }
  967.  
  968. /**********************************************************/
  969. /*  select_label                                          */
  970. /**********************************************************/
  971.  
  972. void select_label(void)
  973. {
  974.     if (Interface)
  975.         GT_SetGadgetAttrs(ListViewGad, Win, NULL,
  976.                                             GTLV_Selected, SelectedLabel,
  977.                                             GTLV_MakeVisible, SelectedLabel,
  978.                                             TAG_END);
  979. }
  980.  
  981. /**********************************************************/
  982. /*  about                                                 */
  983. /**********************************************************/
  984.  
  985. void about(void)
  986. {
  987.     struct EasyStruct Req =
  988.     {
  989.         sizeof(struct EasyStruct),
  990.         0,
  991.         "About",
  992.         "%s\n\nVersion %s (%s)\n\nAuthor: Torsten Kramer\neMail: torstenk@enterp.gun.de\n\nThis program is freeware.\nCopyright 1995 by Torsten Kramer.",
  993.         "OK"
  994.     };
  995.     
  996.     EasyRequest(Win, &Req, NULL, PROG_NAME, PROG_VERSION, PROG_DATE);
  997. }
  998.  
  999. /**********************************************************/
  1000. /*  process_msg                                           */
  1001. /**********************************************************/
  1002.  
  1003. void process_msg(void)
  1004. {
  1005.     struct IntuiMessage    *IMsg;
  1006.  
  1007.     APTR        IAdr;
  1008.     ULONG        MsgClass, MsgID, MsgType,
  1009.                     CxSigFlag, WinSigFlag, TimerSigFlag,
  1010.                     Signals, SigReceived;
  1011.     UWORD        Code;
  1012.     BOOL        Done = TRUE;
  1013.     
  1014.     CxSigFlag            = 1L << BrokerMP->mp_SigBit;
  1015.     TimerSigFlag    = 1L << TimerMP->mp_SigBit;
  1016.  
  1017.     while (Done)
  1018.     {
  1019.         if (Interface)
  1020.         {
  1021.             WinSigFlag    = 1L << Win->UserPort->mp_SigBit;
  1022.             Signals            = SIGBREAKF_CTRL_C | CxSigFlag | TimerSigFlag | WinSigFlag;
  1023.         }
  1024.         else
  1025.         {
  1026.             close_interface();
  1027.             Signals            = SIGBREAKF_CTRL_C | CxSigFlag | TimerSigFlag;
  1028.         }
  1029.  
  1030.         if (RequestSent == FALSE)
  1031.             timer();
  1032.  
  1033.         SigReceived = Wait(Signals);
  1034.  
  1035.         if (SigReceived & TimerSigFlag)
  1036.         {
  1037.             /* Cleanup the port */
  1038.  
  1039.             WaitIO((struct IORequest *)TimerIO);
  1040.  
  1041.             RequestSent = FALSE;
  1042.  
  1043.             if(Interface)
  1044.                 refresh_listview();
  1045.         }
  1046.  
  1047.         if (SigReceived & WinSigFlag)
  1048.         {
  1049.             while (IMsg = GT_GetIMsg(Win->UserPort))
  1050.             {
  1051.                 Code            = IMsg->Code;
  1052.                 MsgClass    = IMsg->Class;
  1053.                 IAdr            = IMsg->IAddress;
  1054.  
  1055.                 GT_ReplyIMsg(IMsg);
  1056.  
  1057.                 switch (MsgClass)
  1058.                 {
  1059.                     case IDCMP_CLOSEWINDOW:
  1060.                         Interface = FALSE;
  1061.                         break;
  1062.  
  1063.                     case IDCMP_MENUPICK:
  1064.                         if (Code != MENUNULL)
  1065.                         {
  1066.                             switch (MENUNUM(Code))
  1067.                             {
  1068.                                 case MENU_PROJECT:
  1069.                                     switch (ITEMNUM(Code))
  1070.                                     {
  1071.                                         case ITEM_HIDE:
  1072.                                             Interface = FALSE;
  1073.                                             break;
  1074.  
  1075.                                         case ITEM_NEWSHELL:
  1076.                                             newshell();
  1077.                                             Interface = FALSE;
  1078.                                             break;
  1079.                                         
  1080.                                         case ITEM_ABOUT:
  1081.                                             about();
  1082.                                             break;
  1083.  
  1084.                                         case ITEM_QUIT:
  1085.                                             Done = FALSE;
  1086.                                             break;
  1087.  
  1088.                                         default:
  1089.                                             break;
  1090.                                     }
  1091.                                     break;
  1092.  
  1093.                                 default:
  1094.                                     break;
  1095.                             }
  1096.                         }
  1097.                         break;
  1098.  
  1099.                     case IDCMP_GADGETUP:
  1100.                         switch (((struct Gadget *)IAdr)->GadgetID)
  1101.                         {
  1102.                             case ID_LISTVIEW:
  1103.                                 click_to_front(Code);
  1104.                                 SelectedLabel = Code;
  1105.                                 break;
  1106.  
  1107.                             default:
  1108.                                 break;
  1109.                         }
  1110.                         break;
  1111.                     
  1112.                     case IDCMP_RAWKEY:
  1113.                         switch (Code)
  1114.                         {
  1115.                             case CURSOR_UP:
  1116.                                 if (SelectedLabel > 0)
  1117.                                     SelectedLabel--;
  1118.                                 
  1119.                                 select_label();
  1120.                                 break;
  1121.                             
  1122.                             case CURSOR_DOWN:
  1123.                                 if (SelectedLabel < (NumLabels - 1))
  1124.                                     SelectedLabel++;
  1125.                                 
  1126.                                 select_label();
  1127.                                 break;
  1128.                             
  1129.                             default:
  1130.                                 break;
  1131.                         }
  1132.                         break;
  1133.  
  1134.                     case IDCMP_VANILLAKEY:
  1135.                         switch (Code)
  1136.                         {
  1137.                             case ESC:
  1138.                                 Interface = FALSE;
  1139.                                 break;
  1140.                                 
  1141.                             case ENTER:
  1142.                                 click_to_front(SelectedLabel);
  1143.                                 break;
  1144.  
  1145.                             default:
  1146.                                 break;
  1147.  
  1148.                         }
  1149.                         break;
  1150.  
  1151.                     case IDCMP_REFRESHWINDOW:
  1152.                         GT_BeginRefresh(Win);
  1153.                         GT_EndRefresh(Win, TRUE);
  1154.                         break;
  1155.  
  1156.                     default:
  1157.                         break;
  1158.                 }
  1159.             }
  1160.         }
  1161.  
  1162.         if (SigReceived & CxSigFlag)
  1163.         {
  1164.             while ((BrokerMsg = (CxMsg *)GetMsg(BrokerMP)))
  1165.             {
  1166.                 MsgID        = CxMsgID(BrokerMsg);
  1167.                 MsgType    = CxMsgType(BrokerMsg);
  1168.  
  1169.                 ReplyMsg((struct Message *)BrokerMsg);
  1170.  
  1171.                 switch (MsgType)
  1172.                 {
  1173.                     case CXM_IEVENT:
  1174.                         switch (MsgID)
  1175.                         {
  1176.                             case EVT_HOTKEY:
  1177.                                 open_interface();
  1178.                                 
  1179.                                 if (Win != NULL)
  1180.                                 {
  1181.                                     WindowToFront(Win);
  1182.                                     ActivateWindow(Win);
  1183.                                     ScreenToFront(Win->WScreen);
  1184.                                 }
  1185.                                 break;
  1186.  
  1187.                             default:
  1188.                                 break;
  1189.                         }
  1190.                         break;
  1191.  
  1192.                     case CXM_COMMAND:
  1193.  
  1194.                         /* Commodities has sent a command */
  1195.  
  1196.                         switch (MsgID)
  1197.                         {
  1198.                             case CXCMD_DISABLE:
  1199.  
  1200.                                 /* The user clicked the Commodities Exchange */
  1201.                                 /* disable gadget.                           */
  1202.  
  1203.                                 ActivateCxObj(Broker, 0);
  1204.                                 CxActive = FALSE;
  1205.                                 break;
  1206.  
  1207.                             case CXCMD_ENABLE:
  1208.  
  1209.                                 /* The user clicked the Commodities Exchange */
  1210.                                 /* enable gadget.                            */
  1211.  
  1212.                                 ActivateCxObj(Broker, 1);
  1213.                                 CxActive = TRUE;
  1214.                                 break;
  1215.  
  1216.                             case CXCMD_KILL:
  1217.  
  1218.                                 /* The user clicked the Commodities Exchange */
  1219.                                 /* kill gadget.                              */
  1220.  
  1221.                                 Done = FALSE;
  1222.                                 break;
  1223.  
  1224.                             case CXCMD_APPEAR:
  1225.  
  1226.                                 /* The user clicked the Commodities Exchange */
  1227.                                 /* 'Show Interface' gadget.                  */
  1228.  
  1229.                                 open_interface();
  1230.                                 
  1231.                                 if (Win != NULL)
  1232.                                 {
  1233.                                     WindowToFront(Win);
  1234.                                     ActivateWindow(Win);
  1235.                                     ScreenToFront(Win->WScreen);
  1236.                                 }
  1237.                                 break;
  1238.  
  1239.                             case CXCMD_DISAPPEAR:
  1240.  
  1241.                                 /* The user clicked the Commodities Exchange */
  1242.                                 /* 'Hide Interface' gadget                   */
  1243.  
  1244.                                 Interface = FALSE;
  1245.                                 break;
  1246.  
  1247.                             case CXCMD_UNIQUE:
  1248.  
  1249.                                 /* The user started our Commodity twice */
  1250.  
  1251.                                 open_interface();
  1252.                                 
  1253.                                 if (Win != NULL)
  1254.                                 {
  1255.                                     WindowToFront(Win);
  1256.                                     ActivateWindow(Win);
  1257.                                     ScreenToFront(Win->WScreen);
  1258.                                 }
  1259.                                 break;
  1260.  
  1261.                             default:
  1262.                                 break;
  1263.                         }
  1264.               break;
  1265.  
  1266.                     default:
  1267.                         break;
  1268.           }
  1269.             }
  1270.         }
  1271.  
  1272.         if (SigReceived & SIGBREAKF_CTRL_C)
  1273.         {
  1274.             Done = FALSE;
  1275.         }
  1276.     }
  1277. }
  1278.